 /**
 * DISCLIMER: 
 * this library is an opensource, feel free to edit or redistribute it
 * but use it with you own risk
 * 
 * ktutnik
 */
package org.ticlib.task
{
	import mx.rpc.events.FaultEvent;
	import flash.events.Event;
	
	public class ConditionalTask extends MultiTask
	{
		private var _comCond: Boolean = false;
		
		[Bindable("completeConditionChange")]
		public function get completeCondition():Boolean
		{
			return _comCond;
		}
		
		public function set completeCondition(value:Boolean):void
		{
			_comCond = value;
			dispatchEvent(new Event("completeConditionChange"));
			
			if(value)
			{
				isComplete = true;
				dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		
		public function ConditionalTask()
		{
			super();
		}
		
		override public function execute():void
		{
			initialize();
			if(tasks.length > 0)
			{
				var task:Task = tasks[0] as Task;
				task.execute();				
			}
		}
		
		override protected function onTaskComplete(e:Event):void
		{
		}
		
		override protected function onTaskFault(e:FaultEvent):void
		{
			if(!continueOnFault) 
			{
				stop();
				dispatchEvent(FaultEvent.createEvent(e.fault));
			}
		}
		
		override protected function onTaskCancel(e:Event):void
		{
			
		}
	}
}